<?php
/**
 * Created by PhpStorm.
 * User: HeiYanHeiMao
 * Date: 2020/4/28
 * Time: 16:11
 */

namespace app\model;


use think\facade\Cache;
use think\facade\Db;
use think\response\Json;
use \Exception as E;

class UserModel
{
    private static $_table = 'eb_user'; // 用户表
    private static $_schoolTable = 'eb_school'; // 学校表
    private static $_floorTable = 'eb_floor'; // 楼层表
    private static $_dormitoryTable = 'eb_dormitory'; // 宿舍楼

    /**列表
     * @param $input
     * @param $token
     * @return Json
     */
    public static function list($input, $token)
    {
        try {
            // 检测当前管理员状态
            $admin = AdminModel::getInfo($token, [1], [2]);
            if ($admin instanceof Json) return $admin;
            $map[] = ['u.operate_id', '=', $admin['id']];
            if ($input['type'] !== '') $map[] = ['u.type', '=', $input['type']];
            if ($input['status'] !== '') $map[] = ['u.status', '=', $input['status']];
            if ($input['school_name'] !== '') $map[] = ['s.name', 'like', "%{$input['school_name']}%"];
            $field = 'u.id,u.type,u.name,u.phone,u.gender,u.delivery_floor,u.create_time,u.floor_id,u.school_id,u.dormitory_id,u.delivery_floor,s.name as school_name,d.name as dormitory_name,f.name as floor_name,u.status,u.remark,u.is_system_order_taking,u.idcard_front,u.idcard_reverse,u.idcard_hold,u.idcard';
            $table = Db::table(self::$_table)->alias('u')
                ->leftJoin([self::$_schoolTable => 's'], 's.id = u.school_id')
                ->leftJoin([self::$_dormitoryTable => 'd'], 'd.id = u.dormitory_id')
                ->leftJoin([self::$_floorTable => 'f'], 'f.id = u.floor_id')
                ->field($field)
                ->where($map);
            $count  = $table->count();
            if ($count == 0) return jsonSuccess('获取成功！', ['count' => $count, 'data' => []]);
            $data = $table->order('u.create_time', 'desc')->page($input['page'],$input['limit'])->select()->toArray();
            return jsonSuccess('获取成功！', ['count' => $count, 'data' => $data]);
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**用户审核（修改状态）
     * @param $input
     * @param $token
     * @return Json
     */
    public static function changeStatus($input, $token)
    {
        $flag = false;
        try {
            // 检测当前管理员状态
            $admin = AdminModel::getInfo($token, [1], [2]);
            if ($admin instanceof Json) return $admin;
            $map = [
                ['id', '=', $input['id']],
                ['operate_id', '=', $admin['id']]
            ];
            if ($input['status'] == 2) {
                $input['remark'] = '';
                $input['is_system_order_taking'] = 1;
            } else {
                $input['is_system_order_taking'] = -1;
            }

            $res = Db::table(self::$_table)->where($map)->update($input);
            if (!$res) {
                Db::rollback();
                return jsonFail('审核失败');
            }
            // 写入缓存
            $rtn = Cache::store('redis')->handler()->hSet('dxsd-user-' . $input['id'], 'is_system_order_taking',$input['is_system_order_taking']);
            if ($rtn === 0) {
                Db::commit();
                return jsonSuccess('审核成功');
            }
            Db::rollback();
            return jsonFail('审核失败');
        } catch (E $e) {
            if ($flag ==  true) Db::rollback();
            return jsonFail('服务内部错误~');
        }
    }

    /**系统接单状态修改
     * @param $input
     * @param $token
     * @return Json
     */
    public static function orderTakingChange($input, $token)
    {
        $flag = false;
        try {
            // 检测当前管理员状态
            $admin = AdminModel::getInfo($token, [1], [2]);
            if ($admin instanceof Json) return $admin;
            $map = [
                ['id', '=', $input['id']],
                ['operate_id', '=', $admin['id']],
                ['type', '=', 2]
            ];
            $res = Db::table(self::$_table)->where($map)->update($input);
            if (!$res) {
                Db::rollback();
                return jsonFail('状态修改失败1');
            }
            // 写入缓存
            $rtn = Cache::store('redis')->handler()->hSet('dxsd-user-' . $input['id'], 'is_system_order_taking',$input['is_system_order_taking']);
            if ($rtn === 0) {
                Db::commit();
                return jsonSuccess('状态修改成功');
            }
            Db::rollback();
            return jsonFail('状态修改失败2');
        } catch (E $e) {
            if ($flag ==  true) Db::rollback();
            return jsonFail('服务内部错误~');
        }
    }
}